//package com.paypalpay.timingtask;
//
//import com.paypalpay.entity.common.UnifiedOrderPackages;
//import com.paypalpay.entity.common.UserboundPaymentOrder;
//import com.paypalpay.pojo.Constant;
//import com.paypalpay.pojo.request.RequestBody.OrderMonthlyPackageRequestBody;
//import com.paypalpay.pojo.request.RequestHeader;
//import com.paypalpay.pojo.request.subRequest.OrderMonthlyPackageRequest;
//import com.paypalpay.pojo.response.subResponse.OrderMonthlyPackageResponse;
//import com.paypalpay.service.*;
//import com.paypalpay.turntointerfaceservice.utils.TurntoInterfaceUtils;
//import com.paypalpay.util.JaxbUtil;
//import com.paypalpay.util.wx.HttpClientTool;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//
//import javax.servlet.http.HttpSession;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.List;
//
///**
// * Demo 微信无卡用户定时去北京下真的orderId的定时任务类
// *
// * @author lm
// * @date 2018/09/9
// */
//@Configuration
//@EnableScheduling
//public class OrderNoDeviceTask {
//    @Autowired
//    private HttpSession session;
//    @Autowired
//    private SequenceService sequenceService;
//
//    @Autowired
//    private UserboundPaymentOrderService userboundPaymentOrderService;
//
//    @Autowired
//    private InternationalPayPackageService internationalPayPackageService;
//
//    @Autowired
//    private UnifiedOrderPackagesService unifiedOrderPackagesService;
//
//    @Autowired
//    PartnerService partnerService;
//
//    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//    String time = df.format(new Date());
//
//    // @Scheduled(cron = "0 0/1 * * * ?") // 每1分钟执行一次
//    // @Scheduled(cron = "0 0 0/2 * * ?") //每两小时整点执行一次
//    public void orderTimingTask() {
//        try {
//            //在数据库获取数据
//            System.out.println("OrderNoDeviceTask微信无卡用户下单定时任务正在执行！");
//            List<UserboundPaymentOrder> userList = userboundPaymentOrderService.getNocardToOrderList();
//            if (userList == null || userList.isEmpty()) {
//                System.out.println("没有查询到需要去下单的数据！");
//            } else {
//                for (UserboundPaymentOrder user : userList) {
//
//                    String partnerCode = user.getPartnerCode();
//                    String secret = partnerService.findSecretByPartnerCode(partnerCode);
//                    String deviceId = user.getDeviceId();
//                    String deviceType = user.getDeviceType();
//                    String packageCode = user.getPackageCode();
//                    //根据前面得到的code去数据库查询name
//                    String packageName = internationalPayPackageService.getPackageNameBypackageCode(packageCode,partnerCode);
//                    String orderPeriod = user.getOrderPeriod();
//                    String orderUnit = user.getOrderUnit();
//                    String payAmount = user.getAmount();
//                    String isOpen = user.getIsOpen();
//
//                    // 用序列给生成一个channelOrderID
//                    String channelOrderID = sequenceService.getchannelOrderIDTaskNo();
//                    System.out.println("后台唯一订单编码是：" + channelOrderID);
//                    // 根据iccid去更新表中的channelOrderID
//                    UserboundPaymentOrder paymentOrder = new UserboundPaymentOrder();
//                    paymentOrder.setChannelOrderID(channelOrderID);
//                    paymentOrder.setDeviceId(deviceId);
//                    // 将channelOrderID更新到数据表里面，为了关联iccid和orderId
//                    userboundPaymentOrderService.updateChannelOrderID(paymentOrder);
//
//                    // 开始去北京下单，请求参数的处理
//                    OrderMonthlyPackageRequestBody body = new OrderMonthlyPackageRequestBody();
//                    body.setDeviceId(deviceId);
//                    body.setDeviceType(deviceType);
//                    body.setPackageName(packageName);
//                    body.setPackageCode(packageCode);
//                    body.setOrderPeriod(orderPeriod);
//                    body.setPayAmount(payAmount);
//                    body.setIsOpen(isOpen);
//
//                    // 请求参数拼接
//                    OrderMonthlyPackageRequest request = new OrderMonthlyPackageRequest();
//                    TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
//                    RequestHeader header = utils.getRequestHeader(Constant.cmd_orderMonthlyPackage, partnerCode);
//                    request.setHeader(header);
//                    request.setBody(body);
//                    String sign = utils.getSign(header, body, body.getClass(), secret);
//                    request.setSign(sign);
//
//                    // 将参数转换成XML格式
//                    String sRequest = new JaxbUtil(OrderMonthlyPackageRequest.class).toXml(request, "utf-8");
//                    System.out.println("请求报文：" + sRequest);
//                    // 发送post请求，并接收返回结果
//                    String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
//
//                    OrderMonthlyPackageResponse responseObj = new JaxbUtil(OrderMonthlyPackageResponse.class).fromXml(result, "utf-8");
//                    String retCode = responseObj.getHeader().getResultCode();
//                    if (false == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
//                        //失败
//                    } else {
//                        // 成功：北京回传的orderId
//                        String orderId = responseObj.getBody().getOrderId();
//                        System.out.println("响应的报文中orderId：" + orderId);
//                        String startData = responseObj.getBody().getOrderStartDate();
//                        String expireDate = responseObj.getBody().getOrderExpireDate();
//                        //1，根据deviceId先去把支付订单orderId的表更新了
//                        UserboundPaymentOrder order = new UserboundPaymentOrder();
//                        order.setOrderId(orderId);
//                        order.setDeviceId(deviceId);
//                        userboundPaymentOrderService.updateOrderIdByDeviceId(order);
//
//                        //2，根据deviceId先去把支付订单orderId等数据的表更新一下
//                        UnifiedOrderPackages unifiedOrder = new UnifiedOrderPackages();
//                        unifiedOrder.setOrderId(orderId);
//                        unifiedOrder.setOrderStartDate(startData);
//                        unifiedOrder.setOrderExpireDate(expireDate);
//                        unifiedOrder.setDeviceId(deviceId);
//                        unifiedOrder.setUpdateTime(time);
//                        unifiedOrderPackagesService.updateOrderInfoByDeviceId(unifiedOrder);
//
//                        System.out.println("OrderNoDeviceTask微信无卡用户下单定时任务执行结束！");
//                    }
//
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}
